<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <link rel="stylesheet" type="text/css" href="css/layer.css">
    <link rel="stylesheet" type="text/css" href="css/swiper.min.css">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/layer.js"></script>
    <script type="text/javascript" src="js/jquery.cookie.js"></script>
    <script src="js/swiper.min.js"></script>
    <script type="text/javascript" src="js/common.js"></script>
</head>
<body>
<div class="nav"></div>
<div class="login-box">
	<div class="login-title">注册</div>
	<div class="login-input">
		<div class="username">
			<label>用户名</label>
			<input type="text" name="username" placeholder="6-16位字母或数字组成" maxlength="16" minlength="6">
		</div>
		<div class="password">
			<label>设置密码</label>
			<input type="password" name="password" placeholder="6-8位字母或数字" maxlength="8" minlength="6">
		</div>
	</div>
	<div class="login-button">注册</div>
</div>
</body>
<script type="text/javascript">
	$(function(){
		// 显示错误信息弹框
	    function layerMsg(msg){
	        layer.open({
	            content: msg,
	            style: 'background-color:rgba(0,0,0,1); color:#fff; border:none;',
	            time: 3
	        }); 
	    }
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return (r[2]);
            return null;
        }
        var url = getQueryString('url');
	    $(".login-button").on("click",function(){
	    	var username = $.trim($('input[name=username]').val());
	    	var password = $.trim($('input[name=password]').val());
            var reg = /^[0-9a-zA-Z]+$/;
	    	if(username == '' || password == '' || username.length <6 || password.length<6 || reg.test(username) == false || reg.test(password) == false){
	    		layerMsg('请填写正确的用户名和密码');
	    	}else{
	    		var formData = new FormData();
                formData.append('username',username);
                formData.append('password',password);
	    		$.ajax({
                    url: window.baseUrl+'api/user/register' ,
                    type: 'POST',
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        if(data.code != 200){
                        	layerMsg(data.message);
                        }else{
                        	// layerMsg('注册成功，去登录！');
                        	// setTimeout(function(){
                        	// 	window.location.href="login.html";
                        	// },2000)
                            $.ajax({
                                url: window.baseUrl+'api/user/login',
                                type: 'POST',
                                data: formData,
                                contentType: false,
                                processData: false,
                                success: function (data) {
                                    if(data.code != "200"){
                                        layerMsg(data.message);
                                    }else{
                                        var token = data.datas.token;
                                        var id = data.datas.id;
                                        $.cookie('token_access',token);
                                        $.cookie('id',id);
                                        $.cookie('username',username);
                                        $.cookie('password',password);
                                        layerMsg('注册并登录成功');
                                        setTimeout(function(){
                                            if(url != null){
                                                window.location.href=url;
                                            }else{
                                                window.location.href='center.html';
                                            }
                                            
                                        },2000)
                                    }
                                }
                            });
                        }
                    }
                });
	    	}
	    })
	})
</script>
</html>